#!/bin/bash

# 设置退出脚本当命令失败时 (非零退出状态)
#set -e

# 函数：运行单元测试
run_unit_tests() {
    local pattern="unit_test_*"
    
    # 遍历所有匹配模式的目录
    for dir in $pattern; do
        if [ -d "$dir" ] && [ -x "$dir/$(basename "$dir")" ]; then
            echo "Running tests in $dir and Collecting coverage data..."
            # 收集代码覆盖率数据
	    (cd "$dir" && "./$(basename "$dir")" && (find ./ -name '*.o' | xargs gcov --preserve-paths))
        else
	    echo "Skipping non-existent or non-executable directory: $dir, $dir/$(basename "$dir")"
        fi
    done
}

rm moc_*
#收集覆盖率报告
find ./ -name "*.o" | xargs gcov

# 上传 result.zip 到平台
upload_result() {
/home/panjinrui/.sonar/sonar-scanner-6.0.0.4432-linux/bin/sonar-scanner \
-Dsonar.projectKey=desktop_kylin-screenshot \
-Dsonar.sources=./unit_test_default \
-Dsonar.host.url=https://codescan.kylinos.cn \
-Dsonar.cfamily.gcov.reportsPath=./unit_test_default \
-Dsonar.token=sqp_af45e69d2d4504552d543847f728f3d55795dd37 \
-Dsonar.branch.name=yhkylin/v11-2503

/home/panjinrui/.sonar/sonar-scanner-6.0.0.4432-linux/bin/sonar-scanner \
-Dsonar.projectKey=desktop_kylin-screenshot \
-Dsonar.sources="./kylin_screenshot/src/tools/text,./kylin_screenshot/src/config,./kylin_screenshot/src/widgets/capture,./kylin_screenshot/src/cli" \
-Dsonar.host.url=https://codescan.kylinos.cn \
-Dsonar.cfamily.gcov.reportsPath=./tests/unit_test_default \
-Dsonar.token=sqp_af45e69d2d4504552d543847f728f3d55795dd37 \
-Dsonar.branch.name=yhkylin/v11-2503 \
-Dsonar.exclusions="**/*.js,**/*.ts,**/*.html,**/*.css,**/*.sh" \
-Dsonar.coverage.exclusions="./kylin_screenshot/src/tools/options/*.h"

/home/panjinrui/.sonar/sonar-scanner-6.0.0.4432-linux/bin/sonar-scanner \
-Dsonar.projectKey=desktop_kylin-screenshot \
-Dsonar.sources="./kylin_screenshot/src/tools/text,./kylin_screenshot/src/config,./kylin_screenshot/src/widgets/capture,./kylin_screenshot/src/cli" \
-Dsonar.host.url=https://codescan.kylinos.cn \
-Dsonar.token=sqp_af45e69d2d4504552d543847f728f3d55795dd37 \
-Dsonar.branch.name=cibot/yhkylin/v11-2503 \
-Dsonar.exclusions="**/*.js,**/*.ts,**/*.html,**/*.css,**/*.sh" \
-Dsonar.coverage.exclusions="./kylin_screenshot/src/tools/options/*.h"


-Dsonar.coverage.exclusions="./kylin_screenshot/src/widgets/panel/**,./kylin_screenshot/src/widgets/*.h,./kylin_screenshot/src/widgets/*.cpp,./kylin_screenshot/src/utils/**,./kylin_screenshot/src/tools/undo/**,./kylin_screenshot/src/tools/start_record/**,./kylin_screenshot/src/tools/sizeindicator/**,./kylin_screenshot/src/tools/selection/**,./kylin_screenshot/src/tools/screenvoice/**,./kylin_screenshot/src/tools/screenmousecursor/**,./kylin_screenshot/src/tools/screenmouse/**,./kylin_screenshot/src/tools/screencut/**,./kylin_screenshot/src/tools/screencapstart/**,./kylin_screenshot/src/tools/screencapoption/**,./kylin_screenshot/src/tools/screencapexit/**,./kylin_screenshot/src/tools/screencap/**,./kylin_screenshot/src/tools/save/**,./kylin_screenshot/src/tools/redo/**,./kylin_screenshot/src/tools/rectangle/**,./kylin_screenshot/src/tools/pin/**,./kylin_screenshot/src/tools/pencil/**,./kylin_screenshot/src/tools/options/**,./kylin_screenshot/src/tools/option_record/**,./kylin_screenshot/src/tools/move/**,./kylin_screenshot/src/tools/marker/**,./kylin_screenshot/src/tools/luping/**,./kylin_screenshot/src/tools/line/**,./kylin_screenshot/src/tools/launcher/**,./kylin_screenshot/src/tools/imgur/**,./kylin_screenshot/src/tools/follow_mouse_record/**,./kylin_screenshot/src/tools/exit/**,./kylin_screenshot/src/tools/cut/**,./kylin_screenshot/src/tools/cursor_record/**,./kylin_screenshot/src/tools/copy/**,./kylin_screenshot/src/tools/circle/**,./kylin_screenshot/src/tools/blur/**,./kylin_screenshot/src/tools/audio_record/**,./kylin_screenshot/src/tools/arrow/**,./kylin_screenshot/src/tools/*.h,./kylin_screenshot/src/tools/*.cpp,./kylin_screenshot/src/third-party/**,./kylin_screenshot/src/record/**,./kylin_screenshot/src/core/**,./kylin_screenshot/src/common/**,./kylin_screenshot/src/*.h"


./kylin_screenshot/src/core,

  sonar-scanner \
    -Dsonar.projectKey=desktop_kylin-screenshot \
    -Dsonar.sources=./kylin_screenshot/src  \
    -Dsonar.host.url=https://codescan.kylinos.cn \
    -Dsonar.coverageReportPaths=./sonarqube-coverage.xml \
    -Dsonar.token=sqp_af45e69d2d4504552d543847f728f3d55795dd37 \
    -Dsonar.branch.name=yhkylin/v11-2503

}

# 编译项目
echo "Compiling the project..."
#qmake && make

# 运行各个单元测试
echo "Running unit tests..."
#run_unit_tests

# 上传到平台
upload_result

echo "All tests have been run successfully, and coverage data has been collected."
